
import os
import time
from django.conf import settings
from django.template import loader
from apps.contents.models import ContentCategory
from apps.contents.utils import get_categories

# 静态化首页
def generate_static_index_html():
    # 获取后台数据库数据
    print('11111111')
    """提供首页广告界面"""
    categories = get_categories()
    # 广告数据显示
    contents = {}
    # 获取广告分类
    ad_categories = ContentCategory.objects.all()
    # 便利
    for cat in ad_categories:
        # 获取数据   父对象.子类名_set.all()
        contents[cat.key] = cat.content_set.filter(status=True).order_by("sequence")
    context = {
        "categories": categories,
        "contents": contents
    }
    # 获取首页模板文件(jinja2模板)
    template = loader.get_template('index.html')
    # 渲染首页html
    html_text = template.render(context)
    # 将首页html字符串写入指定目录，命名为‘文件名.html’
    # 写入文件路径
    file_path = os.path.join(settings.STATICFILES_DIRS[0], 'index22.html')
    with open(file_path,'w') as f:
        f.write(html_text)